package com.nubia.controller;

import com.nubia.entity.Cart;
import com.nubia.entity.PersonShops;
import com.nubia.service.CartService;
import com.nubia.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("cart")
@Slf4j
@CrossOrigin
public class CartController {
    @Autowired
    CartService cartService;

    /**
     * 添加一条购物车
     * @param cart
     * @return
     */

    @RequestMapping("/addOneCart")
    public Map<String,Object> addOneCart(@RequestBody Cart cart){
        if (cart.getUserid() != null && cart.getShopid() != null)
        {
            Cart cart1 = cartService.findCartByBothId(cart);
           // if(cart1.getUser_id() != null && cart1.getShop_id() != null)
            if(cart1 != null)
            {
                return Result.resultDataString("已经添加购物车",false,"请换其他商品");
            }else{
                try {
                    cart.setCart_id(UUID.randomUUID().toString());
                    int i = cartService.addCartByBothId(cart);
                    if(i==1)
                    {
                        return Result.resultDataString("成功",true,"success");
                    }

                    return Result.resultDataString("失败",false,"请换其他商品");
                } catch (Exception e) {
                    e.printStackTrace();
                    return Result.resultDataString("失败",false,"未知异常");
                }
            }

        }else {
            return Result.resultDataString("数据不能为空",false,"error");
        }
    }

    /**
     * 获取用户购物车列表
     * @param cart
     * @return
     */
    @RequestMapping("/getUserCartList")
    public Map<String,Object> ByUseIdGetshopsList(@RequestBody Cart cart){
        if(cart.getUserid() != null)
        {
            try {
                List<PersonShops> cartList = cartService.findUserCartListById(cart);
                return Result.resultData("个人购物车获取成功",true,cartList);
            } catch (Exception e) {
                e.printStackTrace();

                return Result.resultDataString("个人购物车获取成功",false,"系统异常，联系后台");
            }

        }else {
            return Result.resultDataString("个人购物车获取失败",false,"失败");
        }
    }

    /**
     * 根据cartid删除用户购物车 一条商品
     * @param cart
     * @return
     */
    @RequestMapping("/delOneCart")
    public Map<String,Object> delUserOneCart(@RequestBody Cart cart){
        if(cart.getCart_id() != null){
            try {
                int i =  cartService.delCartById(cart.getCart_id());
                return Result.resultDataString("删除个人商品成功",true,"success");
            } catch (Exception e) {
                e.printStackTrace();
                return Result.resultDataString("个人购物车获取成功",false,"系统异常，联系后台");
            }
        }else {
            return Result.resultDataString("删除个人商品失败",false,"失败");
        }
    }





}
